﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Lswweb.Model;

namespace Lswweb.Domain
{
    public class Page : System.Web.UI.Page
    {
        public Page()
        {
            this.Title = SiteSettings.Settings.SiteName;
            this.Description = SiteSettings.Settings.MetaDescription;
            this.Keywords = SiteSettings.Settings.MetaKeywords;
        }

        #region 重载方法

        /// <summary>
        /// 重写出方法使url重写后form的action指向的是生写后的路径。
        /// 这里重载的作用入添加.browser控制效果一样
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer)
        {
            AddMetaContentType();
            base.Render(new RewriteFormHtmlTextWriter(writer));
        }

        public string Description { get; set; }

        public string Keywords { get; set; }

        #endregion#region 重载方法

        public ResourceConfiguration.ResourceStrings  Strings
        {
            get { return ResourceManagement.Content.Strings; }
        }

        public ResourceConfiguration.ResourceStrings Settings
        {
            get { return ResourceManagement.Content.Settings; }
        }

        public SiteUrls Urls
        {
            get { return SiteUrls.Urls; }
        }

        public string GetThemeElement(string elementUrl)
        {
            return Globals.GetSkinPath(elementUrl);
        }


        /// <summary>
        /// 添加编码格式、MetaDescription、MetaKeyWords。
        /// </summary>
        protected virtual void AddMetaContentType()
        {
            if (this.Header == null)
                return;

            //添加编码格式，因为生成的静态页面如果不添加编码格式可能造成乱码。
            HtmlMeta meta = new HtmlMeta();
            meta.HttpEquiv = "content-type";
            meta.Content = Response.ContentType + "; charset=" + Response.ContentEncoding.HeaderName;
            this.Header.Controls.Add(meta);

            HtmlMeta metaDescription = new HtmlMeta();
            metaDescription.Name = "description";
            metaDescription.Content = this.Description;
            this.Header.Controls.Add(metaDescription);

            HtmlMeta metaKeyWord = new HtmlMeta();
            metaKeyWord.Name = "keywords";
            metaKeyWord.Content = this.Keywords;
            this.Header.Controls.Add(metaKeyWord);
        }
    }

    /// <summary>
    /// 管理员
    /// </summary>
    public class PageAdmin : System.Web.UI.MasterPage
    {
        public PageAdmin()
            : base()
        {
            if (!SiteContext.Current.Context.User.Identity.IsAuthenticated)
                SiteContext.Current.Context.Response.Redirect(SiteUrls.Urls.UserLogin(), true);

            if (SiteContext.Current.User == null)
                UserSecurity.SignOut();

            if (!SiteContext.Current.User.IsAdmin)
                SiteContext.Current.Context.Response.Redirect(SiteUrls.Urls.UserMyself(), true);
        }
    }


    /// <summary>
    /// 普通会员
    /// </summary>
    public class PageMember : Lswweb.Domain.Page
    {
        public PageMember()
        {
            if (!SiteContext.Current.Context.User.Identity.IsAuthenticated)
                SiteContext.Current.Context.Response.Redirect(SiteUrls.Urls.UserLogin(), true);

            if (SiteContext.Current.User == null)
                UserSecurity.SignOut();
        }
    }
}
